﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;
using HandyControl.Controls;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;

namespace weighing
{
    /// <summary>
    /// BigVideoWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BigVideoWindow : GlowWindow
    {
        #region variables

        private int oldPreviewHandle;
        private CameraInfoVo mCameraInfo;
       
        #endregion

        public BigVideoWindow(CameraInfoVo vo)
        {
            InitializeComponent();

            mCameraInfo = vo;
            oldPreviewHandle = vo.PreviewHandle;
        }
        #region window
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if(mCameraInfo == null) this.Close();

        }
        private void Window_ContentRendered(object sender, EventArgs e)
        {
            LoginAndPreview();
        }
        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Escape)this.Close();
        }
        private void GlowWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if(mCameraInfo.PreviewHandle > 0)
            {
                mCameraInfo?.SdkHepler.StopPreview(mCameraInfo.PreviewHandle);
                //设置回原来的值，避免之后过磅，截图失败
                mCameraInfo.PreviewHandle = oldPreviewHandle;
            }
        }

        #endregion

        private void LoginAndPreview()
        {
            if (mCameraInfo == null) this.Close();

            int loginHandle = mCameraInfo.Login();

            if (loginHandle > 0)
            {
                mCameraInfo.PictureHandle = VideoPic.Handle;
                mCameraInfo.PreView(out uint errroCode);
                if (mCameraInfo.PreviewHandle < 0)
                {
                    var msg = "视频播放错误：" + errroCode;
                    VideoViewBox.ToolTip = msg;
                }
                else
                {
                    VideoViewBox.ToolTip = "视频播放成功";
                }
            }
            else
            {
                VideoViewBox.ToolTip = "登录失败不能播放";
            }
        }

    }
}
